package com.navidove.appspotlight.model.service.impl;

import java.io.IOException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.navidove.appspotlight.model.bo.SysImage;
import com.navidove.appspotlight.model.dao.SysImageDao;
import com.navidove.appspotlight.model.service.SysImageService;

@Service
public class SysImageServiceImpl implements SysImageService {

	@Autowired
	private SysImageDao sysImageDao;

	public SysImage getSysImage(Long id) {
		return sysImageDao.get(id);
	}

	public SysImage saveSysImage(MultipartFile file) {
		if (file == null || file.isEmpty() || file.getSize() == 0l) {
			return null;
		}

		Date now = new Date();
        SysImage sysImage = new SysImage();
		sysImage.setCrtAt(now);
		

		try {
			sysImage.setContent(file.getBytes());
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
		sysImage.setMime(file.getContentType());
		sysImage.setEnable(Boolean.TRUE);
		sysImage.setFileName(file.getOriginalFilename());
		sysImage.setFileSize((int) file.getSize());
		sysImage.setUpdAt(now);
		sysImageDao.save(sysImage);

		return sysImage;
	}

}
